package com.controller;

import com.pojo.Menus;
import com.pojo.User;
import com.service.MenusService;
import com.service.UserService;
import com.service.impl.MenusServiceImpl;
import com.service.impl.UserServiceImpl;
import com.uilt.BaseController;
import com.uilt.LayuiTable;
import com.uilt.ResPoneEntity;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserController extends BaseController {
    UserService userService = new UserServiceImpl();
    MenusService menusService  = new MenusServiceImpl();

    public void test(int id,String name){
         

    }

    //登录
    public ResPoneEntity tologin(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ResPoneEntity resPoneEntity = new ResPoneEntity();

        response.setCharacterEncoding("utf-8");
        //  1）接受请求参数。
        String uname = request.getParameter("uname");
        String upassword = request.getParameter("upassword");
        //业务
        User user = new User();
        user.setName(uname);
        user.setPassword(upassword);

        User user1 = userService.getUser(user);
        if(user1 !=null){
            HttpSession session=request.getSession();
            session.setAttribute("user",user1);

            resPoneEntity.setState(200);
            resPoneEntity.setMsg("登陆成功");
            resPoneEntity.setObject(user);

            //request.getRequestDispatcher("index.jsp").forward(request,servletResponse);
            //response.sendRedirect("/user?type=getUserAll");
        }
        return resPoneEntity;
    }

    //注册
    public ResPoneEntity addUser(HttpServletRequest req, HttpServletResponse response) throws Exception {

        response.setCharacterEncoding("UTF-8");
        ResPoneEntity resPoneEntity = new ResPoneEntity();
        //接参数
        //req.setCharacterEncoding("utf-8");
        //resp.setCharacterEncoding("utf-8");
        String uname = req.getParameter("uname");
        String upassword = req.getParameter("upassword");
        User user=  new User();
        user.setName(uname);
        user.setPassword(upassword);
        boolean b = userService.insertUser(user);
        if(b){
            resPoneEntity.setState(200);
            resPoneEntity.setMsg("添加成功");
            //response.sendRedirect("/user?type=getUserAll");
        }
        return resPoneEntity;
    }

    //删除
    public ResPoneEntity delUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResPoneEntity resPoneEntity = new ResPoneEntity();
        response.setCharacterEncoding("utf-8");
        //  1）接受请求参数。
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        //业务
        boolean b = userService.delUserById(i);
        if(b){
            resPoneEntity.setState(200);
            resPoneEntity.setMsg("删除成功");
            //重定向：重新发送请求（多次请求）
            //response.sendRedirect("/user?type=getUserAll");
        }
        return resPoneEntity;
    }

    //查询所有
    public String getUserBtn(HttpServletRequest request, HttpServletResponse response){
        response.setCharacterEncoding("utf-8");
        //查询按钮
        String pid1 = request.getParameter("pid");
        int pid = Integer.parseInt(pid1);
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("user");
        List<Menus> menusBtn = menusService.getMenusBtn(user.getId(), pid,2);
        session.setAttribute("menusBtn",menusBtn);
        return "/admin/page/system/user/userList";
    }

    //查询
    public LayuiTable<User> getUserTableAll(HttpServletRequest request, HttpServletResponse response){
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        //业务
        UserService userService = new UserServiceImpl();
        //获取总条数
        List<User> userAll = userService.getUserAll();
        List<User> userAllPage = userService.getUserAll(page,limit);
        LayuiTable<User> layuiTable = new LayuiTable<>();
        layuiTable.setMsg("");
        layuiTable.setCode(0);
        layuiTable.setCount(userAll.size());

        layuiTable.setData(userAllPage);
        return layuiTable;
    }


    //修改回显
    public String getUserById(HttpServletRequest request, HttpServletResponse resp){
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        //获取用户
        User user = userService.getUser(i);
        if(user !=null){
            HttpSession session=request.getSession();
            session.setAttribute("user1",user);
        }
        return "update";
    }

    //修改
    public ResPoneEntity editUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setCharacterEncoding("UTF-8");
        ResPoneEntity resPoneEntity = new ResPoneEntity();
        String uname = req.getParameter("uname");
        String upassword = req.getParameter("upassword");
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);
        User user=  new User();
        user.setName(uname);
        user.setPassword(upassword);
        user.setId(i);
        boolean b = userService.updateUser(user);
        if(b){
            resPoneEntity.setState(200);
            resPoneEntity.setMsg("修改成功");
            //resp.sendRedirect("/user?type=getUserAll");
        }
        return resPoneEntity;
    }

    @Override
    public Class getServletClass() {
        return UserController.class;
    }
}
